package com.henry.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.henry.dao.entity.SysTplCode;
import com.henry.dao.mapper.SysGeneratorDao;
import com.henry.dao.mapper.SysTplCodeMapper;
import com.henry.service.SysTplCodeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.henry.utils.GeneratorCode;
import com.henry.utils.dynamic.ClassNameUtils;
import com.henry.web.vo.Response;
import com.henry.web.vo.SysTplCodeRequestBean;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * <p>
 *   服务实现类
 * </p>
 *
 * @author zhenggui.wu
 * @email 237891668@qq.com
 * @date 2017-05-31 17:52:53
 */
@Service
public class SysTplCodeServiceImpl extends ServiceImpl<SysTplCodeMapper, SysTplCode> implements SysTplCodeService {

    private Logger logger= LoggerFactory.getLogger(SysTplCodeServiceImpl.class);

    @Autowired
    private GeneratorCode generatorCode;
    @Autowired
    private SysGeneratorDao sysGeneratorDao;

    @Override
    public List<SysTplCode> findAllSysTplCode(SysTplCodeRequestBean sysTplCodeRequestBean) {
        SysTplCode sysTplCode=new SysTplCode();
        BeanUtils.copyProperties(sysTplCodeRequestBean,sysTplCode);
        if(StringUtils.isBlank(sysTplCode.getChannelType())){
            sysTplCode.setChannelType("DEFAULT_TPL");
        }
        EntityWrapper<SysTplCode> war=new EntityWrapper();
        war.setEntity(sysTplCode);
        List<SysTplCode> list=this.selectList(war);
        list.forEach(tplCode1 ->tplCode1.setTplContent(generatorCode.converContent(tplCode1.getTplContent())));
        return list;
    }

    @Override
    public Response saveSysTplCode(SysTplCodeRequestBean sysTplCodeRequestBean) {
        Response response=new Response();
        try {
            SysTplCode sysTplCode=new SysTplCode();
            BeanUtils.copyProperties(sysTplCodeRequestBean,sysTplCode);
            this.insert(sysTplCode);
            response.setSuccess();
        }catch (Exception e){
            logger.info(e.getMessage(),e);
            response.setFail();
        }
        return response;
    }

    @Override
    public Response updateSysTplCode(SysTplCodeRequestBean sysTplCodeRequestBean) {
        Response response=new Response();
        try {
            SysTplCode sysTplCode=new SysTplCode();
            BeanUtils.copyProperties(sysTplCodeRequestBean,sysTplCode);
            sysTplCode.setTplContent(generatorCode.revertConverContent(sysTplCode.getTplContent()));
            this.updateById(sysTplCode);
            response.setSuccess();
        }catch (Exception e){
            logger.info(e.getMessage(),e);
            response.setFail();
        }
        return response;
    }

    @Override
    public Response yuLan(SysTplCodeRequestBean sysTplCodeRequestBean) {
        Response response=new Response();

        int count = sysGeneratorDao.checkTableName(sysTplCodeRequestBean.getTableName());
        if (count <= 0) {
            try {
                String content=generatorCode.yuLan(sysTplCodeRequestBean.getTableName(),
                        generatorCode.revertConverContent(sysTplCodeRequestBean.getTplContent()));
                response.setSuccess();
                response.setData(content);
            }catch (Exception e){
                logger.info(e.getMessage(),e);
                response.setFail();
            }
        } else {
            logger.info("表名称存在问题【表名为关键字】！");
            response.setFail();
            response.setMsg("表名称存在问题【表名为关键字】！");
        }
        return response;
    }

    @Override
    public Response deleteSysTplCode(Integer id) {
        Response response=new Response();
        try {
            this.deleteById(id);
            response.setSuccess();
        }catch (Exception e){
            logger.info(e.getMessage(),e);
            response.setFail();
            response.setMsg("删除失败");
        }
        return response;
    }

    @Override
    public List<Map<String, Object>> findAllTables(Map<String, Object> map) {
        return  sysGeneratorDao.queryList(map);
    }

    @Override
    public byte[] generatorCodeZip(String[] tableNames) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        for(String tableName:tableNames){
            generatorCode.zipFileDefault(tableName,zip,null);
        }
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }
}
